/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable no-useless-escape */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { reportToBoss, getAppInfo, getUserInfo } from '../client';
import { getCookie } from '@tencent/gh-qqnews-sdk/gh-qqnews-utils';
import util from '../util';
import { qqnews, qq, weixin, wxWork, tvideo } from '../ua';
import { name as PROJECT_NAME } from '../../../package.json';
import Report, { AttaAdapter, BeaconAdapter, QQNewsAdapter } from '@tencent/gh-qqnews-sdk/gh-qqnews-report';
let PAGE: any = location.pathname.match(/([^\/]+)\.html?$/);
PAGE = PAGE ? PAGE[1] : 'index';
const reporter = new Report();
// reporter.use(new AttaAdapter({ actid: 1 })); // atta上报，每个活动的actid不一样，请与产品进行确认
reporter.use(new BeaconAdapter({
  appkey: 'JS0ZOQ2M3YR2SC',
})); // 灯塔上报
// reporter.use(new QQNewsAdapter({ prefix: 'card2021' })); // 新闻客户端jsapi的上报
const REPORT_CACHE = 'REPORT_CACHE';
const getMediaID = (user: any) => {
  if (user.account === 'qq') {
    return user.qq.mediaid;
  }
  if (user.account === 'wx') {
    return user.qq.mediaid;
  }
  return 'unknown';
};
const getOpenId = (user: any) => {
  if (user.account === 'qq') {
    return getCookie('open_openid');
  }
  if (user.account === 'wx') {
    return getCookie('openid');
  }
  return 'unknown';
};
const getSystemOS = (versionForQQNews: string) => {
  const version = versionForQQNews.split('_');
  if (version[1].toLocaleLowerCase() === 'android') {
    return 'android';
  }
  if (version[1].toLocaleLowerCase() === 'qqnews') {
    return 'ios';
  }
  return 'unknown';
};
const openFromOs = (fromParam = true) => {
  // 判断当前环境
  let openFrom = null;
  if (fromParam) {
    openFrom = util.searchParams('ADTAG');
  }
  if (!openFrom) {
    if (qqnews) {
      openFrom = 'news';
    } else if (weixin) {
      openFrom = 'wx';
    } else if (qq) {
      openFrom = 'qq';
    } else if (wxWork) {
      openFrom = 'wxwork';
    } else if (tvideo) {
      openFrom = 'tvideo';
    } else {
      openFrom = 'unknown';
    }
  }
  return openFrom;
};

// 新闻的默认参数
export const getNewsAppBaseParams = () => Promise.all([getAppInfo(), getUserInfo()]).then((values: any[]) => {
  const [app, user] = values;
  const baseParams = {
    appVersion: app.version ? app.version : 'unknown', // 客户端版本
    systemVersion: app.systemVersion ? app.systemVersion : 'unknown', // 操作系统版本
    appNetwork: app.networkStatus ? app.networkStatus : 'unknown', // 网络类型
    systemOS: getSystemOS(app.versionForQQNews), // 操作系统
    deviceId: app.deviceId ? app.deviceId : 'unknown',
    openid: getOpenId(user),
    omgid: app.omgid ? app.omgid : 'unknown',
    uid: app.uid ? app.uid : 'unknown',
    suid: app.suid ? app.suid : 'unknown',
    QIMEI36: app.QIMEI36 ? app.QIMEI36 : 'unknown',
    qquin: getCookie('uin') ? getCookie('uin') : 'unknown',
    mediaId: getMediaID(user),
    openFrom: openFromOs(false),
    ADTAG: openFromOs(),
  };
  window[REPORT_CACHE] = baseParams;
  return baseParams;
});
// 其他端的默认参数
const getBaseParams = () => new Promise((resolve) => {
  const baseParams = {
    openFrom: openFromOs(false),
    ADTAG: openFromOs(),
  };
  window[REPORT_CACHE] = baseParams;
  resolve(baseParams);
});

const report = (eventName: string, params: {type?: string, eventTime?: number}, direct = false): void => {
  const eventCode = 'common';
  const Params = {
    eventName,
    projectName: PROJECT_NAME,
    page: PAGE,
    ...params,
  };
  if (qqnews) {
    reportToBoss({ appEventId: eventCode, params: Params });
  }
  if (window[REPORT_CACHE]) {
    const baseParams = window[REPORT_CACHE];
    const reportParams = Object.assign({}, Params, baseParams);
    reporter.send({
      event_id: eventCode,
      ...reportParams,
    });
  } else {
    if (qqnews) {
      getNewsAppBaseParams().then(() => {
        const baseParams = window[REPORT_CACHE];
        const reportParams = Object.assign({}, Params, baseParams);
        reporter.send({
          event_id: eventCode,
          ...reportParams,
        });
      });
    } else {
      getBaseParams().then(() => {
        const baseParams = window[REPORT_CACHE];
        const reportParams = Object.assign({}, Params, baseParams);
        reporter.send({
          event_id: eventCode,
          ...reportParams,
        });
      });
    }
  }
};
report('pv', {});
window.addEventListener('load', () => {
  report('load', {});
});
document.addEventListener('DOMContentLoaded', () => {
  report('DOMContentLoaded', {});
});
export default {
  report,
};
